package com.sxit;


import com.sxit.smgp3.SMGP3;
import com.sxit.smgp3.TSMGP_RESP;
import com.sxit.smgp3.TSMGP_SUBMIT;
import com.sxit.smgp3.TSMGP_TLV;

import com.sxit.util.Common;
import com.sxit.util.Utf8Utils;
import org.junit.Test;

import java.util.Map;
import java.util.Random;

/**
 * Created by Administrator on 2015/8/31.
 */
public class SmsUtils {


    public static void main(String[] args) {
        try {
            sendSms(args[0],args[1]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static void sendSms(String mobile,String content) throws Exception{
        try {
             //content=Utf8Utils.utf8ToUnicode(content);
            content=new String(content.getBytes("ISO-10646-UCS-2"));
        }catch (Exception e){
            e.printStackTrace();
            return;
        }
        SMGP3 smgp=new SMGP3();
        int socketID=smgp.SMGP_Connect("115.168.80.52",8891, "handdx", "1065923",(byte)0);

        if (socketID>0)
        {
            int test_ret=smgp.SMGPActiveTest(socketID);
            if (test_ret==0)
            {
                TSMGP_SUBMIT submit=new TSMGP_SUBMIT();
                submit.cMsgType=6;
                submit.cNeedReport=1;
                submit.cPriority=0;
                submit.sServiceID="20006";
                submit.sFeeType="00";
                submit.sFeeCode="0";
                submit.sFixedFee="0";
                submit.sValidTime="";
                submit.sAtTime="";

                submit.sSrcTermID="1065923000005";
                submit.sChargeTermID=mobile;
                submit.sDestTermID=mobile;
                // submit.ucMsgFormat=15;
                submit.ucMsgFormat=8;
                submit.sMsgContent=content;
                submit.sReserve="";

                TSMGP_TLV tlv=new TSMGP_TLV();
                tlv.b_cDestMaskFlag=true;
                tlv.cDestMaskFlag=1;

                tlv.b_cFeeFlag=true;
                tlv.cFeeFlag=1;

                tlv.b_cFeeMaskFlag=true;
                tlv.cFeeMaskFlag=1;

                tlv.b_cMServiceID=true;
                tlv.cMServiceID=1;

                tlv.b_cMsgType=true;
                tlv.cMsgType=1;

                tlv.b_cNodesCount=true;
                tlv.cNodesCount=1;

                tlv.b_cPid=true;
                tlv.cPid=1;

                tlv.b_cPkNumber=true;
                tlv.cPkNumber=1;

                tlv.b_cPkTotal=true;
                tlv.cPkTotal=1;

                tlv.b_cSpDealResult=true;
                tlv.cSpDealResult=1;

                tlv.b_cSpMaskFlag=true;
                tlv.cSpMaskFlag=1;

                tlv.b_cSrcMaskFlag=true;
                tlv.cSrcMaskFlag=1;

                tlv.b_cUdhi=true;
                tlv.cUdhi=1;

                tlv.b_strDestNumberMask=true;
                tlv.strDestNumberMask="123";

                tlv.b_strMsgSrc=true;
                tlv.strMsgSrc="测试TLV";

                tlv.b_strFeeNumberMask=true;
                tlv.strFeeNumberMask="232";

                tlv.b_strLinkId=true;
                tlv.strLinkId="";

                tlv.b_strSrcNumberMask=true;
                tlv.strSrcNumberMask="4333435";

                //submit.tsmgpTlv=tlv;
               // submit.tsmgpTlv=tlv;

                TSMGP_RESP resp=new TSMGP_RESP();
                int submit_ret=smgp.SMGP_Submit(socketID, submit, resp);
                // log.info("发送MT返回值："+submit_ret +" --- seqid:"+resp.getLSerial_ID()+" msgid="+resp.sMsgID);
            }
        }
        smgp.SMGP_Disconnect(socketID);
    }



    /**
     * 创建内容
     * */
    public static String createSMSContent(String content,Map<String,String> params){
        for(String key:params.keySet()){
            content=content.replace(key,params.get(key));
        }
        return content;
    }

    /** 生成数字验证码*/
    public static String getRandomNumber(int len){
        String content="";
        for(int i=0;i<len;i++){
            content=content+ new Random().nextInt(10);
        }
        return content;
    }

}
